#!/bin/sh
echo $0 $*
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}

GAMEDIR="/mnt/SDCARD/Ports/Half-Life"
# GAMEDIR=${PWD}
echo ==========GAMEDIR is $GAMEDIR
controlfolder="../PortMaster"

source $controlfolder/control.txt
#source $controlfolder/tasksetter

get_controls

# 增加错误输出记录
exec > >(tee "$GAMEDIR/log.txt") 2>&1
cd $GAMEDIR

CUR_TTY=/dev/tty0

# Grab text output...
$ESUDO chmod 666 $CUR_TTY
$ESUDO touch log.txt
$ESUDO chmod 666 log.txt
export TERM=linux
printf "\033c" > $CUR_TTY

## Load directly into a mod
RUNMOD="-game bshift"


DEVICE_ARCH="${DEVICE_ARCH:-aarch64}"

$ESUDO chmod 666 /dev/tty1
$ESUDO chmod 666 /dev/uinput
export SDL_GAMECONTROLLERCONFIG="$sdl_controllerconfig"
export LD_LIBRARY_PATH="$GAMEDIR/libs.${DEVICE_ARCH}:$LD_LIBRARY_PATH:/usr/lib32:$GAMEDIR/valve/dlls:$GAMEDIR/valve/cl_dlls"

$GPTOKEYB "xash3d.${DEVICE_ARCH}" &
# 单机模式
# $TASKSET ./xash3d.${DEVICE_ARCH} -ref gles2 -fullscreen -console $RUNMOD 2>&1 | tee -a ./log.txt
# 联机模式
./xash3d -fullscreen -console -sdl_joy_old_api 2>&1 | tee $GAMEDIR/log.txt
$ESUDO kill -9 $(pidof gptokeyb)
unset LD_LIBRARY_PATH
unset SDL_GAMECONTROLLERCONFIG
$ESUDO systemctl restart oga_events &

# Disable console
printf "\033c" >> $CUR_TTY